package com.example.demo.jvm;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexerBase;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer;
import com.alibaba.fastjson.util.JavaBeanInfo;

import java.lang.reflect.Type;

/**
 * @Description fastjson使用asm反序列化
 * @Author BG490350
 * @Date 2023/6/14 15:42
 **/
public class Test_fastjson {

    class MyBean{

        String id;
        String name;
        Integer score;

        public MyBean(String id, String name, Integer score) {
            this.id = id;
            this.name = name;
            this.score = score;
        }

        public MyBean() {
        }
    }



    class FastjsonASMDeserializer_1_MyBean extends JavaBeanDeserializer {
        public char[] id_asm_prefix__ = "\"id\":".toCharArray();
        public char[] name_asm_prefix__ = "\"name\":".toCharArray();
        public char[] score_asm_prefix__ = "\"score\":".toCharArray();

        public FastjsonASMDeserializer_1_MyBean(ParserConfig config, Class<?> clazz) {
            super(config, clazz);
        }

        public FastjsonASMDeserializer_1_MyBean(ParserConfig config, Class<?> clazz, Type type) {
            super(config, clazz, type);
        }

        public FastjsonASMDeserializer_1_MyBean(ParserConfig config, JavaBeanInfo beanInfo) {
            super(config, beanInfo);
        }

        @Override
        public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, int features) {
            JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
            MyBean localMyBean = new MyBean();
            String id = lexer.scanFieldString(this.id_asm_prefix__);
            if (lexer.matchStat > 0) {
                localMyBean.id = id;
            }
            String name = lexer.scanFieldString(this.name_asm_prefix__);
            if (lexer.matchStat > 0) {
                localMyBean.name = name;
            }
            Integer score = lexer.scanFieldInt(this.score_asm_prefix__);
            if (lexer.matchStat > 0) {
                localMyBean.score = score;
            }
            return localMyBean;
        }
    }
}
